package org.zbclan.bf2.rcon.common.server;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.util.Hashtable;

import org.zbclan.bf2.rcon.common.shared.GamePlayObject;

public class ServerModel {

	// /**
	// * SingletonHolder is loaded on the first execution of
	// * Singleton.getInstance() or the first access to
	// SingletonHolder.INSTANCE,
	// * not before.
	// */
	// private static class SingletonHolder {
	// private static final ServerModel INSTANCE = new ServerModel();
	// }
	//
	// public static ServerModel getInstance() {
	// return SingletonHolder.INSTANCE;
	// }
	//
	// Private constructor prevents instantiation from other classes
	// private ServerModel() {
	// }

	private Hashtable<String, Hashtable<String, GamePlayObject>> levels;

	public Hashtable<String, Hashtable<String, GamePlayObject>> getLevels()
			throws IOException {

		if (levels == null) {
			synchronized (this) {
				if (levels == null) {
					levels = new Hashtable<String, Hashtable<String, GamePlayObject>>();

					File[] levelDirArray = Options.BF2_LEVELS_DIR
							.listFiles(new FileFilter() {

								public boolean accept(File pathname) {
									return pathname.isDirectory();
								}
							});

					for (File levelDir : levelDirArray) {
						String name = levelDir.getName();
						levels.put(name,
								new ServerZip(name).getGamePlayObjects());
					}
				}
			}
		}

		return levels;
	}

	// public class Initializer implements ServletContextListener {
	//
	// public void contextDestroyed(ServletContextEvent sce) {
	// }
	//
	// public void contextInitialized(ServletContextEvent sce) {
	//
	// /*
	// * TODO load all levels' game play objects
	// */
	// }
	//
	// }
}
